package com.hankol.community.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baselibrary.base.mvvm.BaseViewModel
import com.baselibrary.bean.FriendListBean
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.SpUtils
import com.hankol.community.api.CommunityApi
import com.hankol.community.bean.BatchUserBean
import com.hankol.community.respository.CommunityRequestManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @description 选择好友
 * @author 关忆北、
 * @time ''2022/4/1'' '1:56 下午'
 */
class SelectFriendViewModel : BaseViewModel() {

    var friendLd: MutableLiveData<List<FriendListBean>> = MutableLiveData()
    var userInfoListLd: MutableLiveData<List<BatchUserBean>> = MutableLiveData()
    fun reqSearchFriend(keyWord: String) {
        CommunityRequestManager.getInstance().reqSearchFriend(keyWord, friendLd)
    }

    fun removeSelectList(currentBean: FriendListBean, selectFriendList: MutableSet<FriendListBean>) {
        for (bean in selectFriendList) {
            if (currentBean.friendId == bean.friendId) {
                selectFriendList.remove(bean)
                return
            }
        }
    }

    fun addSelectList(currentBean: FriendListBean, selectFriendList: MutableSet<FriendListBean>) {
        for (bean in selectFriendList) {
            if (currentBean.friendId == bean.friendId) {
                //已经存在
                return
            }
        }
        selectFriendList.add(currentBean)
    }

    //根据用户idsStr获取用户信息(id 头像 昵称)
    fun reqUidsGetUserInfo(uids: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance()
                    .getApi(CommunityApi::class.java)
                    .reqUidsGetUserInfo(it, uids)
                    ?.subscribeOn(Schedulers.newThread())
                    ?.observeOn(AndroidSchedulers.mainThread())
                    ?.subscribe(object : APIResponse<List<BatchUserBean>>(this) {
                        override fun onSuccess(t: List<BatchUserBean>, json: String) {
                            userInfoListLd.value = t
                        }
                    })
        }
    }

}